<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
    
    function User()
    {
        parent::__construct();
        $this->load->model('user_mod');
        $this->_checktoken();
    }

    public function register()
    {
        $result = array();
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $email = $this->input->post('email');
        if(!empty($username) & !empty($password) & !empty($email)) {
            //check email
            $is_email  = $this->user_mod->get_byemail($email);

            if(!$is_email){ 
                //check username
                $is_user  = $this->user_mod->get_byuser($username);
                if(!$is_user){ 
                    $data_post = array (
                        'email' => $email,
                        'username' => $username,
                        'password' => $this->_encode_password($password),
                        'insert_date' => date('Y-m-d H:i:s')
                    );
            // Upload        
            $config['upload_path'] = './uploads/image/';
            $config['allowed_types'] = 'jpg|png|gif';
            $config['max_size']	= '1024';
            $this->load->library('upload', $config);

            if($this->upload->do_upload('photo')) {
                $dataimg = $this->upload->data();
                $data_post['path_image'] = $dataimg['full_path'];
            }
                    
                    $this->user_mod->add($data_post);
                    $result['result'] = TRUE;
                } else {
                    $result['result'] = FALSE;
                    $result['msg'] = 'username exist';
                }
            } else {
                $result['result'] = FALSE;
                $result['msg'] = 'email exist';
            }
        } else {
            $result['result'] = FALSE;
            $result['msg'] = 'data blank';
        }
        $this->_outputjson($result);
    }
    
    public function login()
    {
        $this->load->model('song_mod');
        $result = array();
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user = $this->user_mod->get_bylogin($username,$this->_encode_password($password));
        if($user) {
            //Insert Action Log
            $this->song_mod->add_log('login', Null, $username);
            $result['result'] = TRUE;
            
            //session
            $data = array(
                    'username' => $username, 
                    'email' => $user['email'],
                    'about' => $user['about'],
                    'path_image' => $user['path_image'],
                    'login' => TRUE
                    );
                
                $this->session->set_userdata($data);
            
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    function logout() {
        $this->session->sess_destroy();
        $result = array();
        $result['message'] = 'Logout Berhasil';
        $result['result'] = TRUE;
        $this->_outputjson($result);
    }
    
    public function update()
    {
        $result = array();
        $username = $this->session->userdata('username');
        $about = $this->input->post('about');
        if(!empty($username) & !empty($about)) {
            $data_post = array (
                'about' => $about
            );
            $config['upload_path'] = './uploads/image/';
            $config['allowed_types'] = 'jpg|png|gif';
            $config['max_size']	= '1024';
            $this->load->library('upload', $config);

            if($this->upload->do_upload('photo')) {
                $dataimg = $this->upload->data();
                $data_post['path_image'] = $dataimg['full_path'];
            }
            $this->user_mod->update($data_post,$username);
            $result['result'] = TRUE;
        } else {
            $result['result'] = FALSE;
        }
    }
    
    public function getinfo()
    {
        $result = array();
        $username = $this->session->userdata('username');
        if(!empty($username)) {
            $datauser  = $this->user_mod->get_byuser($username);
            if($datauser) {
                $result = $datauser;
                $result['result'] = TRUE;
            } else {
                $result['result'] = FALSE;
            }
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function getrecord()
    {
        $result = array();
        $username = $this->session->userdata('username');
        if(!empty($username)) {
            $datarecord  = $this->user_mod->get_user_record($username);
            if($datarecord) {
                $result['records'] = $datarecord;
                $result['result'] = TRUE;
            } else {
                $result['result'] = FALSE;
            }
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    private function _encode_password($string)
    {
        $string .= $this->config->item('encryption_key');
	// Return the SHA-1 encryption
        return sha1($string);
    }
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */